﻿/****************************
文件名:StringUtils.java
创建时间:2008-7-10 上午11:28:13
所在包:com.tools
作者:郑兴苗
说明:防止SQL注入攻击类
****************************/
package net.tycmc.cty.ctyms.timejob.common;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;


public class StringUtils {
   
	//过滤通过页面表单提交的字符
    private static String[][] FilterChars={{"<","&lt;"},{">","&gt;"},{" ","&nbsp;"},{"\"","&quot;"},{"&","&amp;"},{"/","&#47;"},{"\\","&#92;"},{"\n","<br>"}};
 
    //过滤通过javascript脚本处理并提交的字符
    private static String[][] FilterScriptChars={{"\n","\'+\'\\n\'+\'"},{"\r"," "},{"\\","\'+\'\\\\\'+\'"},{"\'","\'+\'\\\'\'+\'"}};

    /**
     * 用特殊的字符连接字符串
     * @param strings 要连接的字符串数组
     * @param spilit_sign 连接字符
     * @return 连接字符串
     */
    public static String stringConnect(String[] strings,String spilit_sign){
      String str="";
      for(int i=0;i<strings.length;i++){
        str+=strings[i]+spilit_sign;
      }
      return str;
    }

    /**
     * 过滤字符串里的的特殊字符
     * @param str 要过滤的字符串
     * @return 过滤后的字符串
     */
    public static String stringFilter(String str){
      String[] str_arr=stringSpilit(str,"");
      for(int i=0;i<str_arr.length;i++){
        for(int j=0;j<FilterChars.length;j++){
          if(FilterChars[j][0].equals(str_arr[i]))
            str_arr[i]=FilterChars[j][1];
        }
      }
      return (stringConnect(str_arr,"")).trim();
    }

    /**
     * 过滤脚本中的特殊字符（包括回车符(\n)和换行符(\r)）
	* @param str 要进行过滤的字符串
	* @return 过滤后的字符串
	* 2004-12-21 闫
	*/
    public static String stringFilterScriptChar(String str){
    	String[] str_arr=stringSpilit(str,"");
    	for(int i=0;i<str_arr.length;i++){
    		for (int j = 0; j < FilterScriptChars.length; j++) {
    			if (FilterScriptChars[j][0].equals(str_arr[i]))
    				str_arr[i] = FilterScriptChars[j][1];
    		}
    	}
    	return(stringConnect(str_arr,"")).trim();
    }

    /**
     * 分割的字符串
     * @param str 要分割的字符串
     * @param spilit_sign 字符串的分割标志
     * @return 分割后得到的字符串数组
     */
    public static String[] stringSpilit(String str,String spilit_sign){
      String[] spilit_string=str.split(spilit_sign);
      if(spilit_string[0].equals(""))
      {
        String[] new_string=new String[spilit_string.length-1];
        for(int i=1;i<spilit_string.length;i++)
          new_string[i-1]=spilit_string[i];
          return new_string;
      }
      else
        return spilit_string;
    }

    /**
     * 字符串字符集转换
     * @param str 要转换的字符串
     * @return 转换过的字符串
     */
    public static String stringTransCharset(String str){
      String new_str=null;
      try{
          new_str=new String(str.getBytes("iso-8859-1"),"GBK");
      }
      catch(Exception e){
        e.printStackTrace();
      }
      return new_str;
    }


    /**
    * 测试字符串处理类
    * @param args 控制台输入参数

   public static void main(String[] args){

     //测试字符串过滤
     String t_str1="<h1>StringDispose字符串处理\n\r\'\"</h1>";
     System.out.println("过滤前："+t_str1);
     System.out.println("过滤后："+StringUtils.stringFilter(t_str1));
     //测试合并字符串
     String[] t_str_arr1={"PG_1","PG_2","PG_3"};
     String t_str2=StringUtils.stringConnect(t_str_arr1,",");
     System.out.println(t_str2);
     //测试拆分字符串
     String[] t_str_arr2=StringUtils.stringSpilit(t_str2,",");
     for(int i=0;i<t_str_arr2.length;i++){
       System.out.println(t_str_arr2[i]);
     }
   }
    */
    public static String ObjectFormat(Object str,int type){
    	if(str==null){
    		if(type==0){
    			return "0";
    		}else if(type==1){
    			return "";
    		}else if(type==2){
    			return DateControl.getDateTime(0);
    		}
    	}
    	return str.toString();
    }
    public static String numberFormat(float str){
    	NumberFormat nf = NumberFormat.getInstance();		
		nf.setMaximumFractionDigits(2);		
		double var = str;
		String retStr = nf.format(var);
		retStr = retStr.replace(",", "");
		return retStr;
    }
    /**
	 * 判断字符串数据是否存在NULL或空值
	 * @version 1.1.1
	 * @author chengshichao
	 */
	public static boolean judgeStr(String[]params){
		if(params==null||params.length==0)return false;
		for(String param:params){
			if(param==null||"".equals(param))return false;
		}
		return true;
	}
	//wangkai add or like方法20140806
	public static  String getDbCode(String dbCode,String PR){
		String sql="";
		if(null==dbCode||"".equals(dbCode)){
			return "";
		}
		String dbCodeArrays[]=dbCode.split(",");
		for(int m=0;m<dbCodeArrays.length;m++){
			if(m==0){
				sql+=" ("+PR+" like '"+dbCodeArrays[m]+"%'";
			}else{
				sql+=" or "+PR+" like '"+dbCodeArrays[m]+"%'";
			}
		}
		sql+=")";
		return sql;
	}
	
	public static String  getOrgCode(String orgCode,String PR){
		if(null==orgCode||"".equals(orgCode)){
			return "";
		}
		String sbOrgCode[] =distinctLikeCodeString(orgCode).split(",");
		String DbCodeSql="";
		for(int i=0;i<sbOrgCode.length;i++){
		if(i==0){
			DbCodeSql+="("+PR+" like '"+sbOrgCode[i]+"%'";
		}else{
			DbCodeSql+=" or "+PR+" like '"+sbOrgCode[i]+"%'";
			}
		}
		DbCodeSql+=")";
		return DbCodeSql;
	}
	//传进去组织机构Code码 字符串，传出来一个新的最高级的字符串。
	public static String distinctLikeCodeString(String OrganizationCodeString) {
		if(null==OrganizationCodeString){
			return ",";
		}
		String OrganizationCodeArrays[] = OrganizationCodeString.split(",");
		String CodeString = "";
		for (int i = 0; i < OrganizationCodeArrays.length; i++) {

			String topCode = SelectTopCode(OrganizationCodeString,
					OrganizationCodeArrays[i]);
			if (distinctCode(topCode, CodeString) == false) {
				CodeString += topCode + ",";
				continue;
			}

		}

		return CodeString;

	}
	public static String SelectTopCode(String OrganizationCodeString,
			String OrganizationCode) {
		String OrganizationCodeArrays[] = OrganizationCodeString.split(",");
		List list = new ArrayList();
		list=selectList(OrganizationCodeString,OrganizationCode);
		
		String curCode = (String) list.get(0);
		for (int i = 1; i < list.size(); i++) {
			if (((String) list.get(i)).length() < curCode.length()) {
				curCode = (String) list.get(i);
				continue;
			}
		}
		return curCode;
	}
	public static boolean distinctCode(String topCode, String CodeString) {
		String CodeStringArrays[] = CodeString.split(",");
		for (int i = 0; i < CodeStringArrays.length; i++) {
			if (topCode.equals(CodeStringArrays[i])) {
				return true;
			}
		}
		return false;
	}
	public static List selectList(String OrganizationCodeString,
			String OrganizationCode) {
		String OrganizationCodeArrays[] = OrganizationCodeString.split(",");
		List list = new ArrayList();
		for (int i = 0; i < OrganizationCodeArrays.length; i++) {
			String orgCode = OrganizationCodeArrays[i];
			if (orgCode.length() < OrganizationCode.length()) {
				if (OrganizationCode.substring(0, orgCode.length()).equals(
						orgCode)) {
					list.add(orgCode);

				}
			} 
			}
		if(list.size()==0){
			list.add(OrganizationCode);
		}
		return list;
		}
	/*end20140806*/

}

